import entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RunWith(JUnit4.class)
public class Demo {
    /**
     * anyMatch(Predicate p) 传入一个断言型函数，对流中所有的元素进行判断，
     * 只要有一个满足条件就返回true，都不满足返回false。
     */
    @Test
    public void test01() {
        List<String> data = Arrays.asList("a", "b", "c", "d");
        boolean a = data.stream()
                .anyMatch(s -> s.startsWith("a"));
        boolean b = data.stream()
                .anyMatch((s) -> s.startsWith("A"));
        System.out.println(a);
        System.out.println(b);
    }

    /**
     * allMatch(Predicate p) 传入一个断言型函数，对流中所有的元素进行判断，
     * 判断条件里的元素，所有的都是，返回true
     */
    @Test
    public void test02() {
        List<String> data = Arrays.asList("a", "b", "c", "d");
        boolean a = data.stream()
                .allMatch((s) -> s.startsWith("a"));
        boolean b = data.stream()
                .allMatch((s) -> true);
        System.out.println(a);
        System.out.println(b);
    }

    /**
     * noneMatch(Predicate p) 传入一个断言型函数，对流中所有的元素进行判断，
     * 与allMatch相反，判断条件里的元素，所有的都不是，返回true
     */
    @Test
    public void test03() {
        List<String> data = Arrays.asList("a", "b", "c", "d");
        boolean a = data.stream()
                .noneMatch((s) -> s.startsWith("a"));
        boolean b = data.stream()
                .noneMatch((s) -> false);
        System.out.println(a);
        System.out.println(b);
    }

    /**
     * GroupingBy进行多个条件组合分组
     */
    @Test
    public void test04() {
        List<User> users = new ArrayList<>();
        users.add(new User("xiaobai", 24, new BigDecimal(11)));
        users.add(new User("xiaobai1", 27, new BigDecimal(25)));
        users.add(new User("xiaobai1", 26, new BigDecimal(25)));
        Optional<List<User>> data = Optional.ofNullable(null);
        users = data.orElseGet(() -> {
            return Collections.emptyList();
        });
        System.out.println(users);
        Map<String, List<User>> collect = users.stream().collect(Collectors.groupingBy(x -> x.getUsername()));
        collect.forEach((k, v) -> {
            System.out.println(k);
            System.out.println(v);
        });
    }

//    Stream.concat(Stream.concat(inspectTasks, zxcfStream),zxqzStream)
//            .sorted(Comparator.comparing(RspHomeTask::getCreateTime).reversed()).skip((current - 1) * size)
//            .limit(size).collect(Collectors.toList());

    /**
     * sorted进行排序
     * reversed排序完取反
     */
    @Test
    public void test05(){
        List<User> users=new ArrayList<>();
        users.add(new User("xiaobai",24,new BigDecimal(11)));
        users.add(new User("xiaobai1",27,new BigDecimal(25)));
        users.add(new User("xiaobai1",26,new BigDecimal(25)));
        List<User> collect = users.stream()
                .sorted(Comparator.comparing(User::getAge).reversed()).
                collect(Collectors.toList());
        System.out.println(collect);
    }

    /**
     * Collectors.joining()进行拼接
     * ()里面是分隔符
     */
    @Test
    public void test06(){
        List<User> users=new ArrayList<>();
        users.add(new User("xiaobai",24,new BigDecimal(11)));
        users.add(new User("xiaobai1",27,new BigDecimal(25)));
        users.add(new User("xiaobai1",26,new BigDecimal(25)));
        String collect = users.stream().map(x -> x.getUsername()).collect(Collectors.joining(","));
        System.out.println(collect);
    }


    /**
     * skip跳过i个对象
     */
    @Test
    public void test07(){
        List<User> users=new ArrayList<>();
        users.add(new User("xiaobai",24,new BigDecimal(11)));
        users.add(new User("xiaobai1",27,new BigDecimal(25)));
        users.add(new User("xiaobai1",26,new BigDecimal(25)));
        users=users.stream().skip(1).collect(Collectors.toList());
        System.out.println(users);
    }

    /**
     * findFirst取第一个对象
     * findAny取任意一个对象 但是不会生效原因是stream是串行流
     * 需要使用并行流就是多线程处理parallelStream处理才会生效
     */
    @Test
    public void test08(){
        List<User> users=new ArrayList<>();
        users.add(new User("xiaobai",24,new BigDecimal(11)));
        users.add(new User("xiaobai1",27,new BigDecimal(25)));
        users.add(new User("xiaobai1",26,new BigDecimal(25)));
        users.add(new User("yangqi",18,new BigDecimal(7000)));
        Optional<User> data = users.stream().findFirst();
        User user = data.orElse(new User());
        System.out.println(user);
        Optional<User> data1 = users.parallelStream().findAny();
        User user1 = data1.orElse(new User());
        System.out.println(user1);
    }

    /**
     * 使用Optional 中的get()方法
     * 最朴素的获取原生value的方法，如果value是空，则直接抛出异常，否则返回。
     */
    @Test
    public void test09(){
        List<User> users=new ArrayList<>();
        users.add(new User("xiaobai",24,new BigDecimal(11)));
        users.add(new User("xiaobai1",27,new BigDecimal(25)));
        users.add(new User("xiaobai1",26,new BigDecimal(25)));
        users.add(new User("yangqi",18,new BigDecimal(7000)));
        Optional<User> data = users.stream().findFirst();
        User user = data.get();
        System.out.println(user);

    }

    /**
     * 使用Optional 中的orElseThrow()方法
     * 如果存在则返回该值，否则为空的话可以抛出自定义的异常。
     */
    @Test
    public void test10(){
        List<User> users=new ArrayList<>();
        users.add(new User("xiaobai",24,new BigDecimal(11)));
        users.add(new User("xiaobai1",27,new BigDecimal(25)));
        users.add(new User("xiaobai1",26,new BigDecimal(25)));
        users.add(new User("yangqi",18,new BigDecimal(7000)));
        Optional<User> data = null;
        User user = data.orElseThrow(()->{
            return new RuntimeException();
        });
        System.out.println(user);

    }


}
